<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>函数的四中调用模式 | JavaScript 高级</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.2.0">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
    

        
    
    
    <link rel="next" href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html" />
    
    
    <link rel="prev" href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="2.2" data-basepath=".." data-revision="Tue Mar 01 2016 06:46:18 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        JavaScript 高级
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="e9c9c586570ff813bf127694b025d155/index.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/c46418c7328d2a8b05cd6ccedd603f88.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        面向对象基础复习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/40a6014bee4d6724e7aa800253818de5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        原型的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/f93b5f4ffff4f7cc386af737772af5d5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        继承的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/9c3f525ec8bdda16f7618bcb3c936b98.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        Object 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3c5d0a2365c2b15c5839ac6e1de9f9ea.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Function 对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/3db73eb5d8866141920eb0d067ba68c3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        作用域链
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/7c0fb98a684b27a818b1b546418cb62c.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        闭包的基本概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
            
                
                    <a href="../e9c9c586570ff813bf127694b025d155/bec81e72f2f0c7d6d17bd137036d3286.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        getter 与 setter
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/index.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        面向对象高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        原型链
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.2" data-path="e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/b8c198488b5b2ca72efdae292161824f.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        函数的四中调用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        对象的创建模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/4fa341af02385d07831cfb4509d5bd20.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        继承的方式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/a8d890e0bae896874f9af984daf86a53.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        闭包案例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/6cc37fe6e9df40a64594e3417c638703.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        正则表达式对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
            
                
                    <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0e3ef80c8612c6c6eb5baa8f397b9494.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        XMLHttpRequest 对象
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >JavaScript 高级</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="%E5%87%BD%E6%95%B0%E7%9A%84%E5%9B%9B%E4%B8%AD%E8%B0%83%E7%94%A8%E6%A8%A1%E5%BC%8F">&#x51FD;&#x6570;&#x7684;&#x56DB;&#x4E2D;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</h1>
<ul>
<li>&#x51FD;&#x6570;&#x7684;&#x56DB;&#x4E2D;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</li>
<li><code>this</code> &#x7684;&#x542B;&#x4E49;</li>
<li>&#x56DB;&#x4E2D;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x7684;&#x5F02;&#x540C;</li>
<li>&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;</li>
<li>&#x4E0A;&#x4E0B;&#x6587;&#x8C03;&#x7528;</li>
</ul>
<p>&#x4E86;&#x89E3;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x6709;&#x52A9;&#x4E8E;&#x6DF1;&#x5165;&#x5B66;&#x4E60;&#x4E0E;&#x5206;&#x6790; JavaScript &#x4EE3;&#x7801;. &#x63A5;&#x4E0B;&#x6765;&#x4E3B;&#x8981;&#x4ECB;&#x7ECD; JavaScript &#x4E2D;&#x51FD;&#x6570;&#x7684;&#x56DB;&#x79CD;&#x4F7F;&#x7528;&#x5F62;&#x5F0F;.</p>
<p>&#x5728; JavaScript &#x4E2D;, &#x51FD;&#x6570;&#x662F;&#x4E00;&#x7B49;&#x516C;&#x6C11;, &#x51FD;&#x6570;&#x5728; JavaScript &#x4E2D;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x7C7B;&#x578B;, &#x800C;&#x975E;&#x50CF; C# &#x6216; &#x5176;&#x4ED6;&#x63CF;&#x8FF0;&#x6027;&#x8BED;&#x8A00;&#x90A3;&#x6837;&#x4EC5;&#x4EC5;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x6765;&#x4F7F;&#x7528;. &#x51FD;&#x6570;&#x6709;&#x56DB;&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;, &#x5206;&#x522B;&#x662F;: &#x51FD;&#x6570;&#x8C03;&#x7528;&#x5F62;
&#x5F0F;, &#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x5F62;&#x5F0F;, &#x6784;&#x9020;&#x5668;&#x5F62;&#x5F0F;, &#x4EE5;&#x53CA; apply &#x5F62;&#x5F0F;. &#x8FD9;&#x91CC;&#x6240;&#x6709;&#x7684;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;, &#x6700;&#x4E3B;&#x8981;&#x7684;&#x533A;&#x522B;&#x5728;
&#x4E8E;&#x5173;&#x952E;&#x5B57; <code>this</code> &#x7684;&#x610F;&#x4E49;. &#x4E0B;&#x9762;&#x5206;&#x522B;&#x4ECB;&#x7ECD;&#x8FD9;&#x4E2A;&#x51E0;&#x79CD;&#x8C03;&#x7528;&#x5F62;&#x5F0F;.</p>
<h2 id="%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E5%BD%A2%E5%BC%8F">&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5F62;&#x5F0F;</h2>
<p>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5F62;&#x5F0F;&#x662F;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x5F62;&#x5F0F;, &#x4E5F;&#x662F;&#x6700;&#x597D;&#x7406;&#x89E3;&#x7684;&#x5F62;&#x5F0F;. &#x6240;&#x8C13;&#x51FD;&#x6570;&#x5F62;&#x5F0F;&#x5C31;&#x662F;&#x4E00;&#x822C;&#x58F0;&#x660E;&#x51FD;&#x6570; &#x540E;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x5373;&#x662F;. &#x4F8B;&#x5982;:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Hello JK!&quot;</span>);
}
func();
</code></pre>
<p>&#x6216;&#x8005;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x848B;&#x5764;, JavaScript &#x9AD8;&#x7EA7;&quot;</span>);
};
func();
</code></pre>
<p>&#x8FD9;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;&#x90FD;&#x4F1A;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x63A7;&#x5236;&#x53F0;&#x4E2D;&#x663E;&#x793A;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x6587;&#x5B57;. &#x8FD9;&#x4E2A;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x8C03;&#x7528;.</p>
<p>&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5F88;&#x7B80;&#x5355;, &#x5C31;&#x662F;&#x5E73;&#x65F6;&#x5B66;&#x4E60;&#x7684;&#x4E00;&#x6837;. &#x8FD9;&#x91CC;&#x7684;&#x5173;&#x952E;&#x662F;, &#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4E2D;, &#x51FD;&#x6570;&#x91CC;&#x7684; <code>this</code> &#x5173;&#x952E;&#x5B57;&#x6307;&#x5168;&#x5C40;&#x5BF9;&#x8C61;, &#x5982;&#x679C;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x5C31;&#x662F; <code>window</code> &#x5BF9;&#x8C61;. &#x4F8B;&#x5982;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>);
};
func();
</code></pre>
<p>&#x6B64;&#x65F6;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160227_53.png" alt=""></p>
<h2 id="%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8%E6%A8%A1%E5%BC%8F">&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</h2>
<p>&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x5F88;&#x7B80;&#x5355;, &#x662F;&#x6700;&#x57FA;&#x672C;&#x7684;&#x8C03;&#x7528;&#x65B9;&#x5F0F;. &#x4F46;&#x662F;&#x540C;&#x6837;&#x7684;&#x662F;&#x51FD;&#x6570;, &#x5C06;&#x5176;&#x8D4B;&#x503C;&#x7ED9;&#x4E00;&#x4E2A;&#x5BF9; &#x8C61;&#x7684;&#x6210;&#x5458;&#x4EE5;&#x540E;, &#x5C31;&#x4E0D;&#x4E00;&#x6837;&#x4E86;. &#x5C06;&#x51FD;&#x6570;&#x8D4B;&#x503C;&#x7ED9;&#x5BF9;&#x8C61;&#x7684;&#x6210;&#x5458;&#x540E;, &#x90A3;&#x4E48;&#x8FD9;&#x4E2A;&#x5C31;&#x4E0D;&#x5728;&#x79F0;&#x4E3A;&#x51FD;&#x6570;, &#x800C;
&#x5E94;&#x8BE5;&#x53EB;&#x505A;&#x65B9;&#x6CD5;. &#x4F8B;&#x5982;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x6211;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E48;?&quot;</span>);
};
<span class="hljs-keyword">var</span> o = {};
o.fn = func; <span class="hljs-comment">// &#x6CE8;&#x610F;&#x6CA1;&#x6709;&#x5706;&#x62EC;&#x53F7;</span>
o.fn(); <span class="hljs-comment">// &#x8C03;&#x7528;</span>
</code></pre>
<p>&#x6B64;&#x65F6;, <code>o.fn</code> &#x5219;&#x662F;&#x65B9;&#x6CD5;, &#x4E0D;&#x662F;&#x51FD;&#x6570;&#x4E86;. &#x5B9E;&#x9645;&#x4E0A; <code>fn</code> &#x7684;&#x65B9;&#x6CD5;&#x4F53;&#x4E0E; <code>func</code> &#x662F;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#x7684;, &#x4F46;&#x662F;&#x8FD9;&#x91CC;&#x6709;&#x4E2A;&#x5FAE;&#x5999;&#x7684;&#x4E0D;&#x540C;. &#x770B;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x63A5;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;</span>
<span class="hljs-built_in">console</span>.log(o.fn === func);
</code></pre>
<p>&#x6253;&#x5370;&#x7ED3;&#x679C;&#x662F; <code>true</code>, &#x8868;&#x660E;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x4E00;&#x6837;&#x7684;&#x4E1C;&#x897F;. &#x4F46;&#x662F;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;, &#x7EC6;&#x5FAE;&#x7684;&#x4E0D;&#x540C;&#x5C31;&#x6709;&#x4E86;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>);
};
<span class="hljs-keyword">var</span> o = {};
o.fn = func;
<span class="hljs-built_in">console</span>.log(o.fn === func); <span class="hljs-comment">// &#x6BD4;&#x8F83;</span>
func(); <span class="hljs-comment">// &#x8C03;&#x7528;</span>
o.fn(); <span class="hljs-comment">// &#x8C03;&#x7528;</span>
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160227_54.png" alt=""></p>
<p>&#x8FD9;&#x91CC;&#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x662F;, &#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x76F8;&#x540C;&#x7684;, &#x56E0;&#x6B64;&#x6253;&#x5370;&#x7ED3;&#x679C;&#x662F; <code>true</code>. &#x4F46;&#x662F;&#x7531;&#x4E8E;&#x4E24;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;
&#x662F;&#x4E0D;&#x4E00;&#x6837;&#x7684;, <code>func</code> &#x7684;&#x8C03;&#x7528;, &#x6253;&#x5370;&#x7684;&#x662F; Window &#x5BF9;&#x8C61;, &#x800C; <code>o.fn</code> &#x7684;&#x6253;&#x5370;&#x7ED3;&#x679C;&#x662F; Object &#x5BF9;&#x8C61;.</p>
<p>&#x8FD9;&#x91CC;&#x4FBF;&#x662F;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x4E0E;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x7684;&#x533A;&#x522B;. &#x51FD;&#x6570;&#x8C03;&#x7528;&#x4E2D;, <code>this</code> &#x4E13;&#x6307;&#x5168;&#x5C40;&#x5BF9;&#x8C61; window, &#x800C; &#x5728;&#x65B9;&#x6CD5;&#x4E2D; <code>this</code> &#x4E13;&#x6307;&#x5F53;&#x524D;&#x5BF9;&#x8C61;. &#x5373; <code>o.fn</code> &#x4E2D;&#x7684; <code>this</code> &#x6307;&#x7684;&#x5C31;&#x662F;&#x5BF9;&#x8C61; <code>o</code>.</p>
<h2 id="%E6%9E%84%E9%80%A0%E5%99%A8%E8%B0%83%E7%94%A8%E6%A8%A1%E5%BC%8F">&#x6784;&#x9020;&#x5668;&#x8C03;&#x7528;&#x6A21;&#x5F0F;</h2>
<p>&#x540C;&#x6837;&#x662F;&#x51FD;&#x6570;, &#x5728;&#x5355;&#x7EAF;&#x7684;&#x51FD;&#x6570;&#x6A21;&#x5F0F;&#x4E0B;, <code>this</code> &#x8868;&#x793A; window; &#x5728;&#x5BF9;&#x8C61;&#x65B9;&#x6CD5;&#x6A21;&#x5F0F;&#x4E0B;, <code>this</code> &#x6307; &#x7684;&#x662F;&#x5F53;&#x524D;&#x5BF9;&#x8C61;. &#x9664;&#x4E86;&#x8FD9;&#x4E24;&#x79CD;&#x60C5;&#x51B5;, JavaScript &#x4E2D;&#x51FD;&#x6570;&#x8FD8;&#x53EF;&#x4EE5;&#x662F;&#x6784;&#x9020;&#x5668;. &#x5C06;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x6784;&#x9020;&#x5668;
&#x6765;&#x4F7F;&#x7528;&#x7684;&#x8BED;&#x6CD5;&#x5C31;&#x662F;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x524D;&#x9762;&#x52A0;&#x4E0A;&#x4E00;&#x4E2A; <code>new</code> &#x5173;&#x952E;&#x5B57;. &#x5982;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;</span>
<span class="hljs-keyword">var</span> Person = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&quot;&#x848B;&#x5764;&quot;</span>;
    <span class="hljs-keyword">this</span>.sayHello = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x4F60;&#x597D;, &#x6211;&#x662F; &quot;</span> + <span class="hljs-keyword">this</span>.name);
    };
};
<span class="hljs-comment">// &#x8C03;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;, &#x521B;&#x5EFA;&#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">var</span> p = <span class="hljs-keyword">new</span> Person();
<span class="hljs-comment">// &#x4F7F;&#x7528;&#x5BF9;&#x8C61;</span>
p.sayHello();
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x6848;&#x4F8B;&#x9996;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570; <code>Person</code>, &#x7136;&#x540E;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x5BF9;&#x8C61; <code>p</code>. &#x8FD9;&#x91CC;&#x4F7F;&#x7528; <code>new</code> &#x8BED;&#x6CD5;. &#x7136;&#x540E;&#x5728;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x8C03;&#x7528; <code>sayHello()</code> &#x65B9;&#x6CD5;. &#x8FD9;&#x4E2A;&#x4F7F;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x6848;&#x4F8B;&#x6BD4;&#x8F83;&#x7B80;&#x5355;. &#x4ECE;&#x6848;&#x4F8B;&#x53EF;&#x4EE5;&#x770B;&#x5230;, &#x6B64;&#x65F6; <code>this</code> &#x6307;&#x7684;&#x662F;&#x5BF9;&#x8C61;&#x672C;&#x8EAB;.</p>
<p>&#x9664;&#x4E86;&#x4E0A;&#x9762;&#x7B80;&#x5355;&#x7684;&#x4F7F;&#x7528;&#x4EE5;&#x5916;, &#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x6784;&#x9020;&#x5668;&#x8FD8;&#x6709;&#x51E0;&#x4E2A;&#x53D8;&#x5316;. &#x5206;&#x522B;&#x4E3A;:</p>
<ol>
<li>&#x6240;&#x6709;&#x9700;&#x8981;&#x7531;&#x5BF9;&#x8C61;&#x4F7F;&#x7528;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5FC5;&#x987B;&#x4F7F;&#x7528; <code>this</code> &#x5F15;&#x5BFC;</li>
<li>&#x51FD;&#x6570;&#x7684; <code>return</code> &#x8BED;&#x53E5;&#x610F;&#x4E49;&#x88AB;&#x6539;&#x5199;&#xFF0C;&#x5982;&#x679C;&#x8FD4;&#x56DE;&#x975E;&#x5BF9;&#x8C61;&#xFF0C;&#x5C31;&#x8FD4;&#x56DE; <code>this</code></li>
</ol>
<h3 id="%E6%9E%84%E9%80%A0%E5%99%A8%E4%B8%AD%E7%9A%84-this">&#x6784;&#x9020;&#x5668;&#x4E2D;&#x7684; <code>this</code></h3>
<p>&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5206;&#x6790;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#x7684;&#x8FC7;&#x7A0B;, &#x65B9;&#x80FD;&#x77E5;&#x9053; <code>this</code> &#x7684;&#x610F;&#x4E49;. &#x5982;&#x4E0B;&#x9762;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> Person = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&quot;jk&quot;</span>;
};
<span class="hljs-keyword">var</span> p = <span class="hljs-keyword">new</span> Person();
</code></pre>
<p>&#x8FD9;&#x91CC;&#x9996;&#x5148;&#x5B9A;&#x4E49;&#x4E86;&#x51FD;&#x6570; <code>Person</code>, &#x4E0B;&#x9762;&#x5206;&#x6790;&#x4E00;&#x4E0B;&#x6574;&#x4E2A;&#x6267;&#x884C;:</p>
<ol>
<li>&#x7A0B;&#x5E8F;&#x5728;&#x6267;&#x884C;&#x5230;&#x8FD9;&#x4E00;&#x53E5;&#x7684;&#x65F6;&#x5019;, &#x4E0D;&#x4F1A;&#x6267;&#x884C;&#x51FD;&#x6570;&#x4F53;, &#x56E0;&#x6B64; JavaScript &#x7684;&#x89E3;&#x91CA;&#x5668;&#x5E76;&#x4E0D;&#x77E5;&#x9053;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5185;&#x5BB9;</li>
<li>&#x63A5;&#x4E0B;&#x6765;&#x6267;&#x884C; <code>new</code> &#x5173;&#x952E;&#x5B57;, &#x521B;&#x5EFA;&#x5BF9;&#x8C61;, &#x89E3;&#x91CA;&#x5668;&#x5F00;&#x8F9F;&#x5185;&#x5B58;, &#x5F97;&#x5230;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;, &#x5C06;&#x65B0;&#x5BF9;&#x8C61;&#x7684;&#x5F15;&#x7528;&#x4EA4;&#x7ED9;&#x51FD;&#x6570;</li>
<li>&#x7D27;&#x63A5;&#x7740;&#x6267;&#x884C;&#x51FD;&#x6570;, &#x5C06;&#x4F20;&#x8FC7;&#x6765;&#x7684;&#x5BF9;&#x8C61;&#x5F15;&#x7528;&#x4EA4;&#x7ED9; <code>this</code>. &#x4E5F;&#x5C31;&#x662F;&#x8BF4;, &#x5728;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;, <code>this</code> &#x5C31;&#x662F;&#x521A;&#x521A;&#x88AB; <code>new</code> &#x521B;&#x5EFA;&#x51FA;&#x6765;&#x7684;&#x5BF9;&#x8C61;</li>
<li>&#x7136;&#x540E;&#x4E3A; <code>this</code> &#x6DFB;&#x52A0;&#x6210;&#x5458;, &#x4E5F;&#x5C31;&#x662F;&#x4E3A;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x6210;&#x5458;</li>
<li>&#x6700;&#x540E;&#x51FD;&#x6570;&#x7ED3;&#x675F;, &#x8FD4;&#x56DE; <code>this</code>, &#x5C06; <code>this</code> &#x4EA4;&#x7ED9;&#x5DE6;&#x8FB9;&#x7684;&#x53D8;&#x91CF;</li>
</ol>
<p>&#x5206;&#x6790;&#x8FC7;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x4EE5;&#x540E;, &#x53EF;&#x4EE5;&#x5F97;&#x5230;, &#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x7684; <code>this</code> &#x5C31;&#x662F;&#x5F53;&#x524D;&#x5BF9;&#x8C61;.</p>
<h3 id="%E6%9E%84%E9%80%A0%E5%99%A8%E4%B8%AD%E7%9A%84-return">&#x6784;&#x9020;&#x5668;&#x4E2D;&#x7684; <code>return</code></h3>
<p>&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D; <code>return</code> &#x7684;&#x610F;&#x4E49;&#x53D1;&#x751F;&#x4E86;&#x53D8;&#x5316;, &#x9996;&#x5148;&#x5982;&#x679C;&#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;, &#x5982;&#x679C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x5BF9; &#x8C61;, &#x90A3;&#x4E48;&#x5C31;&#x4FDD;&#x7559;&#x539F;&#x610F;. &#x5982;&#x679C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x975E;&#x5BF9;&#x8C61;, &#x6BD4;&#x5982;&#x6570;&#x5B57;, &#x5E03;&#x5C14;&#x548C;&#x5B57;&#x7B26;&#x4E32;, &#x90A3;&#x4E48;&#x5C31;&#x8FD4;&#x56DE; <code>this</code>, &#x5982;&#x679C;&#x6CA1;&#x6709; <code>return</code> &#x8BED;&#x53E5;, &#x90A3;&#x4E48;&#x4E5F;&#x8FD4;&#x56DE; <code>this</code>. &#x770B;&#x4E0B;&#x9762;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctr = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&quot;jk&quot;</span>;
    <span class="hljs-keyword">return</span> {
        name: <span class="hljs-string">&quot;&#x848B;&#x5764;&quot;</span>
    };
};
<span class="hljs-keyword">var</span> p = <span class="hljs-keyword">new</span> ctr();
<span class="hljs-built_in">console</span>.log(p.name);
</code></pre>
<p>&#x6267;&#x884C;&#x4EE3;&#x7801;, &#x8FD9;&#x91CC;&#x6253;&#x5370;&#x7684;&#x7ED3;&#x679C;&#x662F; &quot;&#x848B;&#x5764;&quot;. &#x56E0;&#x4E3A;&#x6784;&#x9020;&#x65B9;&#x6CD5;&#x4E2D;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;, &#x90A3;&#x4E48;&#x4FDD;&#x7559; <code>return</code> &#x7684;&#x610F;&#x4E49;, &#x8FD4;&#x56DE;&#x5185;&#x5BB9;&#x4E3A; <code>return</code> &#x540E;&#x9762;&#x7684;&#x5BF9;&#x8C61;. &#x518D;&#x770B;&#x4E0B;&#x9762;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> ctr = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&quot;jk&quot;</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;&#x848B;&#x5764;&quot;</span>;
};
<span class="hljs-keyword">var</span> p = <span class="hljs-keyword">new</span> ctr();
<span class="hljs-built_in">console</span>.log(p);
<span class="hljs-built_in">console</span>.log(p.name);
</code></pre>
<p>&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;:</p>
<p><img src="../images/Snip20160227_56.png" alt=""></p>
<p>&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x662F;, &#x5148;&#x5F39;&#x6253;&#x5370; <code>ctr {name: &quot;jk&quot;}</code>, &#x7136;&#x540E;&#x6253;&#x5370; &quot;jk&quot;. &#x56E0;&#x4E3A;&#x8FD9;&#x91CC; <code>return</code> &#x7684;&#x662F;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;, &#x5C5E;&#x4E8E;&#x57FA;&#x672C;&#x7C7B;&#x578B;, &#x90A3;&#x4E48;&#x8FD9;&#x91CC;&#x7684; <code>return</code> &#x8BED;&#x53E5;&#x65E0;&#x6548;, &#x8FD4;&#x56DE;&#x7684;&#x662F; <code>this</code> &#x5BF9;&#x8C61;. &#x56E0;&#x6B64;&#x7B2C;&#x4E00;&#x4E2A;&#x6253;&#x5370;&#x7684;&#x662F; ctr &#x5BF9;&#x8C61;, &#x800C;&#x7B2C;&#x4E8C;&#x4E2A;&#x4E0D;&#x4F1A;&#x6253;&#x5370; <code>undefined</code>.</p>
<h2 id="-apply-%E8%B0%83%E7%94%A8%E6%A8%A1%E5%BC%8F"><code>Apply</code> &#x8C03;&#x7528;&#x6A21;&#x5F0F;</h2>
<p>&#x9664;&#x4E86;&#x4E0A;&#x8FF0;&#x4E09;&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;&#x4EE5;&#x5916;, &#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x5BF9;&#x8C61;&#x8FD8;&#x6709; <code>apply</code> &#x65B9;&#x6CD5;&#x4E0E; <code>call</code> &#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;. &#x8FD9;&#x4FBF;&#x662F;&#x7B2C;&#x56DB;&#x79CD;&#x8C03;&#x7528;&#x6A21;&#x5F0F;, &#x6211;&#x79F0;&#x5176;&#x4E3A; apply &#x6A21;&#x5F0F;.</p>
<p>&#x9996;&#x5148;&#x4ECB;&#x7ECD; apply &#x6A21;&#x5F0F;, &#x9996;&#x5148;&#x8FD9;&#x91CC; apply &#x6A21;&#x5F0F;&#x65E2;&#x53EF;&#x4EE5;&#x50CF;&#x51FD;&#x6570;&#x4E00;&#x6837;&#x4F7F;&#x7528;, &#x4E5F;&#x53EF;&#x4EE5;&#x50CF;&#x65B9;&#x6CD5;&#x4E00;&#x6837;&#x4F7F;&#x7528; &#x53EF;&#x4EE5;&#x8BF4;&#x662F;&#x4E00;&#x4E2A;&#x7075;&#x6D3B;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;. &#x9996;&#x5148;&#x770B;&#x770B;&#x8BED;&#x6CD5;:</p>
<pre><code class="lang-javascript">&#x51FD;&#x6570;&#x540D;.apply(&#x5BF9;&#x8C61;, &#x53C2;&#x6570;&#x6570;&#x7EC4;);
</code></pre>
<p>&#x8FD9;&#x91CC;&#x770B;&#x8BED;&#x6CD5;&#x6BD4;&#x8F83;&#x6666;&#x6DA9;, &#x8FD8;&#x662F;&#x4F7F;&#x7528;&#x6848;&#x4F8B;&#x6765;&#x8BF4;&#x660E;:</p>
<ul>
<li>&#x65B0;&#x5EFA;&#x4E24;&#x4E2A; js &#x6587;&#x4EF6;&#xFF0C;&#x5206;&#x522B;&#x4E3A; &quot;js1.js&quot; &#x4E0E; &quot;js2.js&quot;</li>
<li>&#x6DFB;&#x52A0;&#x4EE3;&#x7801;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-comment">// js1.js &#x6587;&#x4EF6;</span>
<span class="hljs-keyword">var</span> func1 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&quot;jk&quot;</span>;
};
func1.apply(<span class="hljs-literal">null</span>);
<span class="hljs-built_in">console</span>.log(name);

<span class="hljs-comment">// js2.js &#x6587;&#x4EF6;</span>
<span class="hljs-keyword">var</span> func2 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&quot;jk&quot;</span>;
};
<span class="hljs-keyword">var</span> o = {};
func2.apply(o);
<span class="hljs-built_in">console</span>.log(o.name);
</code></pre>
<ul>
<li>&#x5206;&#x522B;&#x8FD0;&#x884C;&#x7740;&#x4E24;&#x6BB5;&#x4EE3;&#x7801;, &#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x7B2C;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x7684; <code>name</code> &#x5C5E;&#x6027;&#x5DF2;&#x7ECF;&#x52A0;&#x8F7D;&#x5230;&#x5168;&#x5C40;&#x5BF9;&#x8C61; window &#x4E2D;. &#x800C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6587;&#x4EF6;&#x4E2D;&#x7684; <code>name</code> &#x5C5E;&#x6027;&#x662F;&#x5728;&#x4F20;&#x5165;&#x7684;&#x5BF9;&#x8C61; <code>o</code> &#x4E2D;. &#x5373;&#x7B2C;&#x4E00;&#x4E2A;&#x76F8;&#x5F53;&#x4E8E;&#x51FD;&#x6570;&#x8C03;&#x7528;, &#x7B2C;&#x4E8C;&#x4E2A;&#x76F8;&#x5F53;&#x4E8E;&#x65B9;&#x6CD5;&#x8C03;&#x7528;</li>
</ul>
<p>&#x8FD9;&#x91CC;&#x7684;&#x53C2;&#x6570;&#x662F;&#x65B9;&#x6CD5;&#x672C;&#x8EAB;&#x6240;&#x5E26;&#x7684;&#x53C2;&#x6570;, &#x4F46;&#x662F;&#x9700;&#x8981;&#x7528;&#x6570;&#x7EC4;&#x7684;&#x5F62;&#x5F0F;&#x5B58;&#x50A8;&#x5728;. &#x6BD4;&#x5982;&#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> arr1 = [ <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, [ <span class="hljs-number">4</span>, <span class="hljs-number">5</span> ], [ <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span> ] ];
<span class="hljs-comment">// &#x5C06;&#x6570;&#x7EC4;&#x6241;&#x5E73;&#x5316;</span>
<span class="hljs-keyword">var</span> arr2 = arr1.concat.apply([], arr1);
</code></pre>
<p>&#x7136;&#x540E;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B; call &#x6A21;&#x5F0F;. call &#x6A21;&#x5F0F;&#x4E0E; apply &#x6A21;&#x5F0F;&#x6700;&#x5927;&#x7684;&#x4E0D;&#x540C;&#x5728;&#x4E8E; call &#x4E2D;&#x7684;&#x53C2;&#x6570;&#x4E0D;&#x7528;&#x6570;&#x7EC4;. &#x770B;&#x4E0B;&#x9762;&#x4EE3;&#x7801;&#x5C31;&#x6E05;&#x695A;&#x4E86;:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x65B9;&#x6CD5;</span>
<span class="hljs-keyword">var</span> func = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">name, age, gender</span>) </span>{
    <span class="hljs-keyword">this</span>.name = name;
    <span class="hljs-keyword">this</span>.age = age;
    <span class="hljs-keyword">this</span>.gender = gender;
};
<span class="hljs-comment">// &#x521B;&#x5EFA;&#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">var</span> o1 = {}, o2 = {};
<span class="hljs-comment">// &#x7ED9;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x6210;&#x5458;</span>
<span class="hljs-comment">// apply &#x6A21;&#x5F0F;</span>
<span class="hljs-keyword">var</span> p1 = func.apply(o1, [<span class="hljs-string">&quot;jk&quot;</span>, <span class="hljs-number">19</span>, <span class="hljs-string">&quot;male&quot;</span>]);
<span class="hljs-comment">// call &#x6A21;&#x5F0F;</span>
<span class="hljs-keyword">var</span> p2 = func.call(o2, <span class="hljs-string">&quot;baby&quot;</span>, <span class="hljs-number">20</span>, <span class="hljs-string">&quot;female&quot;</span>);
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;, apply &#x6A21;&#x5F0F;&#x4E0E; call &#x6A21;&#x5F0F;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;.</p>
<p>&#x5B9E;&#x9645;&#x4E0A;, &#x4F7F;&#x7528; apply &#x6A21;&#x5F0F;&#x548C; call &#x6A21;&#x5F0F;, &#x53EF;&#x4EE5;&#x4EFB;&#x610F;&#x7684;&#x64CD;&#x4F5C;&#x63A7;&#x5236; <code>this</code> &#x7684;&#x610F;&#x4E49;, &#x5728;&#x51FD;&#x6570; js &#x7684;&#x8BBE;&#x8BA1;&#x6A21;&#x5F0F;&#x4E2D;&#x4F7F;&#x7528;&#x5E7F;&#x6CDB;. &#x7B80;&#x5355;&#x5C0F;&#x7ED3;&#x4E00;&#x4E0B;, js &#x4E2D;&#x7684;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x6709;&#x56DB;&#x79CD;&#x6A21;&#x5F0F;, &#x5206;&#x522B;&#x662F;: &#x51FD;&#x6570;&#x5F0F;, &#x65B9;&#x6CD5;&#x5F0F;, &#x6784;&#x9020;&#x5668;&#x5F0F;&#x548C; apply &#x5F0F;. &#x800C;&#x8FD9;&#x4E9B;&#x6A21;&#x5F0F;&#x4E2D;, <code>this</code> &#x7684;&#x542B;&#x4E49;&#x5206;&#x522B;&#x4E3A;: &#x5728;&#x51FD;&#x6570;&#x4E2D; <code>this</code> &#x662F;&#x5168;&#x5C40;&#x5BF9;&#x8C61; window, &#x5728;&#x65B9;&#x6CD5;&#x4E2D; <code>this</code> &#x6307;&#x5F53;&#x524D;&#x5BF9;&#x8C61;, &#x5728;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D; <code>this</code> &#x662F;&#x88AB;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;, &#x5728; apply &#x6A21;&#x5F0F;&#x4E2D; <code>this</code> &#x53EF;&#x4EE5;&#x968F;&#x610F;&#x7684;&#x6307;&#x5B9A;. &#x5728; apply &#x6A21;&#x5F0F;&#x4E2D;&#x5982;&#x679C;&#x4F7F;&#x7528; <code>null</code>, &#x5C31;&#x662F;&#x51FD;&#x6570;&#x6A21;&#x5F0F;, &#x5982;&#x679C;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;, &#x5C31;&#x662F;&#x65B9;&#x6CD5;&#x6A21;&#x5F0F;.</p>
<h2 id="%E6%A1%88%E4%BE%8B">&#x6848;&#x4F8B;</h2>
<p>&#x4E0B;&#x9762;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x6848;&#x4F8B;&#x7ED3;&#x675F;&#x672C;&#x7BC7;&#x5427;. &#x6848;&#x4F8B;&#x8BF4;&#x660E;: &#x6709;&#x4E00;&#x4E2A; div, id &#x4E3A; dv, &#x9F20;&#x6807;&#x79FB;&#x5230;&#x4E0A;&#x9762;&#x53BB;&#x9AD8;&#x5EA6;&#x589E;&#x5927; 2 &#x500D;, &#x9F20;&#x6807;&#x79BB;&#x5F00;&#x6062;&#x590D;. &#x4E0B;&#x9762;&#x76F4;&#x63A5;&#x4E0A; js &#x4EE3;&#x7801;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> dv = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&quot;dv&quot;</span>);
<span class="hljs-keyword">var</span> height = <span class="hljs-built_in">parseInt</span>(dv.style.height || dv.offsetHeight);
<span class="hljs-keyword">var</span> intervalId;
dv.onmouseover = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// &#x505C;&#x6B62;&#x5DF2;&#x7ECF;&#x5728;&#x6267;&#x884C;&#x7684;&#x52A8;&#x753B;</span>
    clearInterval(intervalId);
    <span class="hljs-comment">// &#x5F97;&#x5230;&#x76EE;&#x6807;&#x9AD8;&#x5EA6;</span>
    <span class="hljs-keyword">var</span> toHeight = height * <span class="hljs-number">2</span>;
    <span class="hljs-comment">// &#x83B7;&#x5F97;&#x5F53;&#x524D;&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">var</span> that = <span class="hljs-keyword">this</span>;
    <span class="hljs-comment">// &#x5F00;&#x5668;&#x8BA1;&#x65F6;&#x5668;&#xFF0C;&#x7F13;&#x6162;&#x53D8;&#x5316;</span>
    intervalId = setInterval(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-comment">// &#x5F97;&#x5230;&#x73B0;&#x5728;&#x7684;&#x9AD8;&#x5EA6;</span>
        <span class="hljs-keyword">var</span> height = <span class="hljs-built_in">parseInt</span>(dv.style.height || dv.offsetHeight);
        <span class="hljs-comment">// &#x8BB0;&#x5F55;&#x6BCF;&#x6B21;&#x9700;&#x8981;&#x53D8;&#x5316;&#x7684;&#x6B65;&#x957F;</span>
        <span class="hljs-keyword">var</span> h = <span class="hljs-built_in">Math</span>.ceil(<span class="hljs-built_in">Math</span>.abs(height - toHeight) / <span class="hljs-number">10</span>);
        <span class="hljs-comment">// &#x5224;&#x65AD;&#x53D8;&#x5316;&#xFF0C;&#x5982;&#x679C;&#x6B65;&#x957F;&#x4E3A;0&#x5C31;&#x505C;&#x6B62;&#x8BA1;&#x65F6;&#x5668;</span>
        <span class="hljs-keyword">if</span>( h &gt; <span class="hljs-number">0</span> ) {
            <span class="hljs-comment">// &#x8FD9;&#x91CC;&#x4E3A;&#x4EC0;&#x4E48;&#x8981;&#x7528;that&#x5462;&#xFF1F;&#x601D;&#x8003;&#x4E00;&#x4E0B;&#x5427;</span>
            that.style.height = (height + h) + <span class="hljs-string">&quot;px&quot;</span>;
        } <span class="hljs-keyword">else</span> {
            clearInterval(intervalId);
        }
    }, <span class="hljs-number">20</span>);
};
dv.onmouseout = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// &#x539F;&#x7406;&#x548C;&#x4E4B;&#x524D;&#x4E00;&#x6837;</span>
    clearInterval(intervalId);
    <span class="hljs-keyword">var</span> toHeight = height;
    <span class="hljs-keyword">var</span> that = <span class="hljs-keyword">this</span>;
    intervalId = setInterval(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">var</span> height = <span class="hljs-built_in">parseInt</span>(dv.style.height || dv.offsetHeight);
        <span class="hljs-keyword">var</span> h = <span class="hljs-built_in">Math</span>.ceil(<span class="hljs-built_in">Math</span>.abs(height - toHeight) / <span class="hljs-number">10</span>);
        <span class="hljs-keyword">if</span>( h &gt; <span class="hljs-number">0</span> ) {
            that.style.height = (height - h) + <span class="hljs-string">&quot;px&quot;</span>;
        } <span class="hljs-keyword">else</span> {
            clearInterval(intervalId);
        }
    }, <span class="hljs-number">20</span>);
};
</code></pre>
<h2 id="%E5%B0%8F%E7%BB%93">&#x5C0F;&#x7ED3;</h2>
<ul>
<li>&#x51FD;&#x6570;&#x7684;&#x56DB;&#x4E2D;&#x8C03;&#x7528;&#x65B9;&#x5F0F;</li>
<li><code>this</code> &#x7684;&#x542B;&#x4E49;</li>
<li>apply &#x4E0E; call &#x6A21;&#x5F0F;&#x7684;&#x533A;&#x522B;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../e2bb52a93f2a04d3848c6bc059d4442d/5d3938919f20993f4b502f77cb59536f.html" class="navigation navigation-prev " aria-label="Previous page: 原型链"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../e2bb52a93f2a04d3848c6bc059d4442d/0f37e1ef8250b257649a6d584bd33fb6.html" class="navigation navigation-next " aria-label="Next page: 对象的创建模式"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
